package com.atsansan.mywork.login.controller;

import com.atsansan.mywork.login.entity.UserEntity;
import com.atsansan.mywork.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author sansan
 * @create 2021-01-22 14:21
 */
@Controller
@RequestMapping("user/user")
public class RegistContrller {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/regist")
    public String regist(@RequestParam("name") String name,
                         @RequestParam("password") String password,
                         @RequestParam("phone") String phone, Map<String,String> map){
        UserEntity userEntity = new UserEntity();
        List<UserEntity> list = userService.list();
        if(list.stream().filter(userEntity1 -> userEntity1.getName().equals(name)).findAny().isPresent()){
//            return "redirect:/regist";
            return "redirect:/error3";
        }else {
            userEntity.setName(name);
            userEntity.setPassword(password);
            userEntity.setPhone(phone);
            userService.save(userEntity);
            return "redirect:/login";
        }


    }

}
